package MapBase.logic.untils
{
	import XComponent.hide.XEvent;
	import XComponent.hide.XFileReference;
	
	import com.friendsofed.isometric.MapLoader;
	
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;

	public class MapConfig
	{
		private var _mapLoader : MapLoader;
		
		private var _completeCallBack : Function;
		
		public var mapXML : XML;
		public var mapData : Dictionary;
		
		private var m_xFile : XFileReference;
		
		public function MapConfig()
		{

		}

		public function saveConfig(xml : XML,xmlName : String,callBack : Function = null) : void
		{
			if(!xml) return;
			
			m_xFile = new XFileReference;
			m_xFile.save(xml,xmlName+".xml",callBack);
		}
		
		public function openConfig(callBack : Function = null) : void
		{
			_completeCallBack = callBack;
			
			m_xFile = new XFileReference;
			m_xFile.open();
			
			m_xFile.addEventListener(XFileReference.LOADSELECTCOMPLETE,OnLoadMapDataComplete);
		}
		
		private function OnLoadMapDataComplete(evt : XEvent):void
		{
			m_xFile.removeEventListener(XFileReference.LOADSELECTCOMPLETE,OnLoadMapDataComplete);
			
			mapXML = new XML(evt.data);
			praseXML();
		
			if(_completeCallBack != null)
			{
				_completeCallBack();
			}
		}
		
		public function openByURL(url : String,callBack : Function = null):void
		{
			_completeCallBack = callBack;
			
			_mapLoader = new MapLoader;
			_mapLoader.loadMap(url);
			_mapLoader.addEventListener(Event.COMPLETE,OnLoadMapComplete);
		}
		
		private function OnLoadMapComplete(evt : Event):void
		{
			_mapLoader.removeEventListener(Event.COMPLETE,OnLoadMapComplete);
			
			mapXML = _mapLoader.mapXML;
			mapData = _mapLoader.mapData;
			
			_completeCallBack();
			
			_mapLoader = null;
			_completeCallBack = null;
		}
		
		private function destory() : void
		{
			_mapLoader = 			null;
			
			_completeCallBack = 	null;
			
			mapXML = 				null;
			mapData = 				null;
			
			m_xFile = 				null;
		}
		
		public function praseAgain(xml:XML):void
		{
			mapXML = xml;
			praseXML();
		}
		
		private function praseXML() : void
		{
			mapData = new Dictionary;
			
			for each(var mapInfo : XML in mapXML.elements())
			{
				if(mapInfo.name()["localName"] != "isoBox") 
				{
					mapData[mapInfo.name()["localName"]] = mapInfo.text().toString();
				}
				else
				{
					mapData["isoBox"] = new Array;
					
					for each(var boxInfo : XML in mapInfo.elements())
					{
						var box : Object = new Object;
						box["id"] = boxInfo.@id.toString();
						box["isoX"] = Number(boxInfo.@isoX);
						box["isoZ"] = Number(boxInfo.@isoZ);
						
						mapData["isoBox"].push(box);
					}
				}
			}
		}
	}
}